<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

if($class_pi->user_type!='Super Administrator'){
	echo "<script> alert('you need to be logged in as a super administrator to edit the Pages-and-Items config.'); window.history.go(-1); </script>";
	exit();
}

$field_id = $class_pi->get_var('field_id', '' );

if(!$field_id){
	//start new
		
	$plugin = $class_pi->get_var('plugin', '' );
	$type_id = $class_pi->get_var('type_id', '' );
	$name = '';	
	$custom_field = '';	
	$field_params['description'] = '';
	$field_params['alert_message'] = '';
	$field_params['default_value'] = '';
	$field_params['validation'] = 0;
		
	//end new
	
}else{
	//start edit
	
	$class_pi->db->setQuery("SELECT * FROM #__pi_custom_fields WHERE id='$field_id' LIMIT 1");
	$custom_fields = $class_pi->db->loadObjectList();	
	$custom_field = $custom_fields[0];
	$name = $custom_field->name;	
	$type_id = $custom_field->type_id;
	$plugin = $custom_field->plugin;
	$temp = $custom_field->params;
	//$temp = stripslashes($temp);
	
	//echo $temp;
	//explode params	
	$temp = explode( '[;-)# ]', $temp);
	for($n = 0; $n < count($temp); $n++){	
		//list($var,$value) = split("-=-",$temp[$n]);		
		//$field_params[$var] = trim($value); 		
		$temp2 = explode('-=-',$temp[$n]);
		$var = $temp2[0];
		$value = '';
		if(count($temp2)==2){
			$value = $temp2[1];
		}
		$field_params[$var] = trim($value);
	}	
	
	
					
	//end edit
}

//ADD MS 08.09.2009
if(defined('_JEXEC'))
{
	$field_type = $plugin;
	$pluginParams = new JParameter( $class_pi->get_plugin_params_base($plugin) );
	if($pluginParams->get('version'))
	{
		$field_type .= '<br />'._pi_lang_version.': '.$pluginParams->get('version');
	}
	if($pluginParams->get('required_pi_version'))
	{
		if($class_pi->version >= $pluginParams->get('required_pi_version'))
		{
			$field_type .= '<br />'._pi_lang_version_ok;
		}
		else
		{
			$field_type .= '<br />'._pi_lang_version_not_ok_1.$pluginParams->get('required_pi_version').' '._pi_lang_version_not_ok_2.$class_pi->version ;
		}
	}
}
//ADD MS END

//get itemtype name
$itemtype_name = 'custom_'.$type_id;

//get number of items which need updating	
$class_pi->db->setQuery( "SELECT c.id "
."FROM #__pi_item_index AS i "		
."LEFT JOIN #__content AS c "
."ON c.id=i.item_id "
."WHERE i.itemtype='$itemtype_name' "
."AND (c.state='0' OR c.state='1') "	
);
			
$items_array = $class_pi->db->loadObjectList();
$items_of_this_type = count($items_array);
	
	
?>
<script src="../includes/js/overlib_mini.js" language="JavaScript" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="components/com_pi_pages_and_items/css/pages_and_items3.css" />
<?php
//if Joomla 1.0.x do some style changes
if(!defined('_JEXEC')){
	//joomla 1.0.x
	echo '<link rel="stylesheet" type="text/css" href="components/com_pi_pages_and_items/css/joomla_1_0_x.css" />';
}

?>
<script language="JavaScript" type="text/javascript">

function do_pre_submit_checks(){
	if (document.adminForm.name.value == '') {
		alert('<?php echo addslashes(_pi_lang_no_name); ?>');					
		return;
	} else {
		if(check_name_field()){				
			items_of_this_type = document.getElementById('items_of_this_type').value;
			if(items_of_this_type!=0){
				if(confirm("<?php echo addslashes(_pi_lang_want_to_update_items); ?>")){
					document.getElementById('update_items').value = '1';
				}
			}					
			submitform('config_custom_itemtype_field_save');
		}
	}
}

function submitbutton(pressbutton) {
	if (pressbutton == 'config_custom_itemtype_field_save') {
		do_pre_submit_checks();	
	}
	if (pressbutton == 'config_custom_itemtype_field_apply') {
		document.getElementById('sub_task').value = 'apply';
		do_pre_submit_checks();
	}		
	if (pressbutton == 'cancel') {
		document.location.href = 'index2.php?option=com_pi_pages_and_items&task=config_custom_itemtype&type_id=<?php echo $type_id; ?>';		
	}
}

Array.prototype.in_array = function (element) 
{
  var retur = false;
  for (var values in this) 
  {
    if (this[values] == element) 
    {
      retur = true;
      break;
    }  
  }
  return retur;
};

function check_name_field()
{
	name = document.getElementById('name').value;
	name_format = true;	
	if(!name.match(/^[a-zA-z1234567890\u0021\u0023\u002D\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF]+$/) || name.match(/\u005F/))
	{
		alert('<?php echo addslashes(_pi_lang_only_char); ?>');
		document.getElementById('name').focus();
		name_format = false;	
	}
	return name_format;
}


</script>
<?php

//give headers in Joomla 1.5 a bit more spunk
$class_pi->spunk_up_headers_1_5();

//print_r($class_pi->pi_config);

?>

<div style="margin: 0 auto; width: 950px; text-align: left;">	
	<form name="adminForm" method="post" action="">
		<input type="hidden" name="option" value="com_pi_pages_and_items" />
		<input type="hidden" name="task" value="config_custom_itemtype_field_save" />	
		<input type="hidden" name="sub_task" id="sub_task" value="" />
		<input type="hidden" name="field_id" value="<?php echo $field_id; ?>" />		
		<input type="hidden" name="type_id" value="<?php echo $type_id; ?>" />
		<input type="hidden" name="plugin" value="<?php echo $plugin; ?>" />
		<input type="hidden" name="update_items" id="update_items" value="0" />
		<input type="hidden" name="items_of_this_type" id="items_of_this_type" value="<?php echo $items_of_this_type; ?>" />
		
		<a href="index2.php?option=com_pi_pages_and_items">pages and items</a> > <a href="index2.php?option=com_pi_pages_and_items&task=config&tab=itemtypes"><?php echo _pi_lang_config; ?></a> > <a href="index2.php?option=com_pi_pages_and_items&task=config_custom_itemtype&type_id=<?php echo $type_id; ?>"><?php echo _pi_lang_custom_itemtype_config; ?></a> > <?php echo _pi_lang_custom_itemtype_field_config; ?>
		<h2><?php echo _pi_lang_custom_itemtype_field_config; ?></h2>	
		<div class="pi_form_wrapper">
			<div class="pi_width20">
				<?php echo _pi_lang_field_type; ?>:
			</div>
			<div class="pi_width70">
				<?php echo $field_type;
				/*
				CHANGE MS 08.09.2009
				echo $plugin;  to echo $field_type;
				*/
				?>				
			</div>
		</div>
		<div class="pi_form_wrapper">
			<div class="pi_width20">
				<?php echo _pi_lang_field_name; ?>: <img src="components/com_pi_pages_and_items/images/star.gif" alt="required field" />
			</div>
			<div class="pi_width70">
				<input type="text" class="width200" value="<?php echo $name; ?>" id="name" name="name"  onchange="check_name_field();" /> <?php echo _pi_lang_only_char; ?>
			</div>
		</div>	
		<?php		
		
		//get language for fieldtype plugin, defaults to english
		if(!in_array($plugin, $class_pi->fieldtypes_integrated)){
			if(file_exists(dirname(__FILE__).'/../../../../'.$class_pi->plugin_dir.'/pages_and_items/fieldtypes/'.$plugin.'/language/'.$class_pi->pi_config['language'].'.php')){
				require_once(dirname(__FILE__).'/../../../../'.$class_pi->plugin_dir.'/pages_and_items/fieldtypes/'.$plugin.'/language/'.$class_pi->pi_config['language'].'.php'); 
			}else{				
				if(file_exists(dirname(__FILE__).'/../../../../'.$class_pi->plugin_dir.'/pages_and_items/fieldtypes/'.$plugin.'/language/english.php')){
					require_once(dirname(__FILE__).'/../../../../'.$class_pi->plugin_dir.'/pages_and_items/fieldtypes/'.$plugin.'/language/english.php'); 
				}
			}
		}

		//include field plugin class
		$class_pi->include_field_class($plugin);
		$class_name = 'class_fieldtype_'.$plugin;		
		$class_plugin = new $class_name();
		echo $class_plugin->display_config_form($plugin, $type_id, $name, $field_params, $field_id);		
		
		?>
	</form>		
</div>

<?php $class_pi->display_footer(); ?>